import {
  ArrayMinSize,
  IsArray,
  IsEnum, IsInstance, IsInt,
  IsJSON,
  IsNotEmpty, IsNumber,
  IsOptional,
  IsString,
  IsUUID, Max,
  MaxLength, Min
} from "class-validator";
import { ApiProperty, ApiPropertyOptional, OmitType, PartialType } from "@nestjs/swagger";
import { BooleanNumber, EvaluationCategory, EvaluationStatus } from "common/utils/data";
import { BasePageDto, BaseScrollPageDto } from "@/common/base/base.dto";
import { Type } from "class-transformer";

class EvaluationIndexDtoSpecificationItem{
  @IsNotEmpty()
  @ApiProperty()
  @IsUUID()
  specificationUuid: string;

  @IsNotEmpty()
  @ApiProperty()
  @IsArray()
  value: (string | number)[];
}

export class EvaluationIndexDto extends BaseScrollPageDto {
  @IsOptional()
  @ApiPropertyOptional({
    description: '关键词，模糊搜品牌、标题，忽略大小写',
    example: '索尼',
  })
  @IsString()
  keyword?: string;

  @IsOptional()
  @ApiPropertyOptional({
    description: '多个uuid精准查询,逗号分割',
    example: '280c0b2f-80b5-4720-843a-adce2d2242dd,bec9a9a5-f2ed-4654-993d-b383c8e8756e',
  })
  @IsString()
  uuids?: string;

  @IsOptional()
  @ApiPropertyOptional({
    description: '分类，是耳机还是播放器',
    example: 'EARBUDS',
  })
  @IsEnum(EvaluationCategory)
  category?: EvaluationCategory;

  @IsOptional()
  @ApiPropertyOptional({
    description: '规格参数',
    example: '[{"specificationUuid":"280c0b2f-80b5-4720-843a-adce2d2242dd","value":["1000","1001"]}]',
    type: [EvaluationIndexDtoSpecificationItem],
  })
  @IsArray()
  @IsInstance(EvaluationIndexDtoSpecificationItem,{each: true})
  @Type(()=>EvaluationIndexDtoSpecificationItem)
  specifications?: EvaluationIndexDtoSpecificationItem[];

  @IsOptional()
  @ApiPropertyOptional({
    description: '品牌uuid',
    example: ['280c0b2f-80b5-4720-843a-adce2d2242dd','bec9a9a5-f2ed-4654-993d-b383c8e8756e'],
  })
  @IsArray()
  brandUuids?: string[];

  @IsOptional()
  @ApiPropertyOptional({
    description: '价格区间，多个区间查询，null为不限',
    example: [[null,800],[1000,1001]],
    type: 'array',
    items: {
      type: 'array',
      items: {
        oneOf: [
          { 
            type: 'number',
          },
          { 
            type: 'null',
          }
        ]
      },
    },
  })
  @IsArray()
  prices?: [(number | null), (number | null)][];
}

export class EvaluationBatchTitleDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  evaluationUuids?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  evaluationComposeUuids?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  eqUuids?: string;
}

export class EvaluationBatchDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsArray()
  @IsUUID(undefined, {
    each: true,
  })
  evaluationUuids?: string[];

  @IsOptional()
  @ApiPropertyOptional()
  @IsArray()
  @IsUUID(undefined, {
    each: true,
  })
  evaluationComposeUuids?: string[];

  @IsOptional()
  @ApiPropertyOptional()
  @IsArray()
  @IsUUID(undefined, {
    each: true,
  })
  eqUuids?: string[];
}

export class EvaluationAdminIndexDto extends BasePageDto {
  @IsOptional()
  @ApiPropertyOptional()
  @IsEnum(EvaluationCategory)
  category?: EvaluationCategory;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  title?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsEnum(EvaluationStatus)
  status?: EvaluationStatus;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  brandUuids?: string;
}

export class EvaluationAdminCreateDataItem {
  @IsNotEmpty()
  @ApiProperty()
  @IsUUID()
  projectUuid: string;

  @IsNotEmpty()
  @ApiProperty()
  @IsEnum(BooleanNumber)
  defaultShow: BooleanNumber;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(999999)
  data?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(5000)
  desc?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(200)
  name?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(200)
  group?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(200)
  img?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsJSON()
  json?: any;
}

export class EvaluationAdminCreateSpecificationItem {
  @IsNotEmpty()
  @ApiProperty()
  @IsUUID()
  specificationUuid: string;

  @IsNotEmpty()
  @ApiProperty()
  @IsString()
  @MaxLength(500)
  value: string;
}

export class EvaluationAdminCreateFileItem {
  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(200)
  group?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(200)
  url?: string;
}

export class EvaluationAdminCreateDto{
  @IsNotEmpty()
  @ApiProperty()
  @IsString()
  @MaxLength(50)
  title: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsArray()
  keywords?: string[];

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  @MaxLength(1000)
  desc?: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsArray()
  thumbnails?: string[];

  @IsNotEmpty()
  @ApiProperty({
    type: [EvaluationAdminCreateDataItem],
  })
  @IsArray()
  @ArrayMinSize(1)
  @IsInstance(EvaluationAdminCreateDataItem,{each: true})
  @Type(()=>EvaluationAdminCreateDataItem)
  datas: EvaluationAdminCreateDataItem[]

  @IsOptional()
  @ApiPropertyOptional({
    type: [EvaluationAdminCreateSpecificationItem],
  })
  @IsArray()
  @IsInstance(EvaluationAdminCreateSpecificationItem,{each: true})
  @Type(()=>EvaluationAdminCreateSpecificationItem)
  specifications?: EvaluationAdminCreateSpecificationItem[]

  @IsNotEmpty()
  @ApiProperty()
  @IsUUID()
  brandUuid: string;

  @IsOptional()
  @ApiPropertyOptional()
  @IsNumber()
  price?: number;

  @IsOptional()
  @ApiPropertyOptional()
  @IsString()
  buyUrl?: string;

  @IsNotEmpty()
  @ApiProperty()
  @IsEnum(EvaluationCategory)
  category: EvaluationCategory;

  @IsOptional()
  @ApiPropertyOptional({
    type: [EvaluationAdminCreateFileItem],
  })
  @IsArray()
  @IsInstance(EvaluationAdminCreateFileItem,{each: true})
  @Type(()=>EvaluationAdminCreateFileItem)
  impulseResponseFiles?: EvaluationAdminCreateFileItem[]
}

export class EvaluationAdminUpdateDto extends PartialType(EvaluationAdminCreateDto){
  @IsOptional()
  @ApiPropertyOptional()
  @IsEnum(EvaluationStatus)
  status?: EvaluationStatus;

  @IsOptional()
  @ApiPropertyOptional()
  @IsInt()
  @Max(1)
  @Min(0)
  isStick?: number;
}

export class EvaluationOpenApiIndexDto extends BaseScrollPageDto { // 继承OmitType方法直接丢了方法
  @IsOptional()
  @ApiPropertyOptional({
    description: '多个uuid精准查询',
    example: ['280c0b2f-80b5-4720-843a-adce2d2242dd','bec9a9a5-f2ed-4654-993d-b383c8e8756e'],
  })
  @IsArray()
  uuids?: string[];

  @IsOptional()
  @ApiPropertyOptional({
    description: '关键词，模糊搜品牌、标题，忽略大小写',
    example: '索尼',
  })
  @IsString()
  keyword?: string;

  @IsOptional()
  @ApiPropertyOptional({
    description: '分类，是耳机还是播放器',
    example: 'EARBUDS',
  })
  @IsEnum(EvaluationCategory)
  category?: EvaluationCategory;

  @IsOptional()
  @ApiPropertyOptional({
    description: '规格参数',
    example: '[{"specificationUuid":"280c0b2f-80b5-4720-843a-adce2d2242dd","value":["1000","1001"]}]',
    type: [EvaluationIndexDtoSpecificationItem],
  })
  @IsArray()
  @IsInstance(EvaluationIndexDtoSpecificationItem,{each: true})
  @Type(()=>EvaluationIndexDtoSpecificationItem)
  specifications?: EvaluationIndexDtoSpecificationItem[];

  @IsOptional()
  @ApiPropertyOptional({
    description: '品牌uuid',
    example: ['280c0b2f-80b5-4720-843a-adce2d2242dd','bec9a9a5-f2ed-4654-993d-b383c8e8756e'],
  })
  @IsArray()
  brandUuids?: string[];

  @IsOptional()
  @ApiPropertyOptional({
    description: '价格区间，多个区间查询，null为不限',
    example: [[null,800],[1000,1001]],
    type: 'array',
    items: {
      type: 'array',
      items: {
        oneOf: [
          { 
            type: 'number',
          },
          { 
            type: 'null',
          }
        ]
      },
    },
  })
  @IsArray()
  prices?: [(number | null), (number | null)][];
}